package cn.ffcs.uec.selfstaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecWorkEditBean;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uom.common.util.PubUtil;

@Controller
@Scope("prototype")
public class UecSelfWorkEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecWorkEditBean bean = new UecWorkEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecWork uecWork;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 */
	public void onCreate$uecWorkEditWindow() {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {

	}

	/**
	 * bindBean
	 */
	public void bindBean() {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecWorkEditWindow().setTitle("新增人员履历记录");
			bean.getUecWorkEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecWorkEditWindow().setTitle("查看人员履历记录");
			uecWork = (UecWork) arg.get("uecWork");
			if (null != uecWork) {
				PubUtil.fillBeanFromPo(uecWork, bean);
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecWorkEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecWorkEditWindow().setTitle("编辑人员履历记录");
			bean.getUecWorkEditDiv().setHeight("30%");
			uecWork = (UecWork) arg.get("uecWork");
			if (null != uecWork) {
				PubUtil.fillBeanFromPo(uecWork, bean);
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecWork = UecWork.newInstance();
			PubUtil.fillPoFromBean(bean, uecWork);
			uecWork.setStaffId(uecStaffInfo.getStaffId());
			uecWork.addOnly();
			Events.postEvent("onOK", this.self, uecWork);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecWork);
			uecWork.updateOnly();
			Events.postEvent("onOK", this.self, uecWork);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecWorkEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}

}
